/**
 * Created by caozengling on 2017/10/25.
 */
var $, element, layer, laydate, form;
var billID = QueryUtils.GetQueryString("BILL_ID");
layui.use(['jquery', 'element', 'layer', 'laydate', 'form', 'table'], function () {
    $ = layui.jquery, element = layui.element, layer = layui.layer, laydate = layui.laydate, form = layui.form, table = layui.table;

    //加载 租金 电费 押金 等 的明细
    //租金
    var type = QueryUtils.GetQueryString("type");
    var payment_logo = QueryUtils.GetQueryString("payment_logo");//账单 支出 收款 标识
    //alert(type);
    $(".zujin").html("<div class=\"layui-col-xs6\">\n" +
        "                                <label class=\"layui-form-label1\">房屋<span class='yingshou'>应收</span>" + type + "</label>\n" +
        "                                <div class=\"layui-inline\">\n" +
        "                                    <input type=\"text\" name=\"TOTAL_PRICE\" id=\"RENT\" lay-verify=\"title\" placeholder=\"\"\n" +
        "                                           class=\"layui-input\" readonly>\n" +
        "                                    <span class=\"label-money\">元</span>\n" +
        "                                </div>\n" +
        "                            </div>\n" +
        "                            <div class=\"layui-col-xs6\">\n" +
        "                                <label class=\"layui-form-label1\">收费类型</label>\n" +
        "                                <div class=\"layui-inline\">" +
        "                                <input type=\"text\" class=\"layui-input\" id=\"select1\" name=\"COLLECTION_TYPE\" placeholder=\"\" readonly>\n" +
        "                                    <span class=\"label-money\"></span>\n" +
        "                                </div>\n" +
        "                            </div>");
    if("1"==payment_logo){
        $(".yingshou").html("应付");
    }
    if ("0" == type) {
        $(".zujin").html("<div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">房屋<span class='yingshou'>应收</span>租金</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" name=\"TOTAL_PRICE\" id=\"RENT\" lay-verify=\"title\" placeholder=\"\"\n" +
            "                                           class=\"layui-input\" readonly>\n" +
            "                                    <span class=\"label-money\">元</span>\n" +
            "                                </div>\n" +
            "                            </div>\n" +
            "                            <div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">收费类型</label>\n" +
            "                                <div class=\"layui-inline\">" +
            "                                <input type=\"text\" class=\"layui-input\" id=\"select1\" name=\"COLLECTION_TYPE\" placeholder=\"\" readonly>\n" +
            "                                    <span class=\"label-money\"></span>\n" +
            "                                </div>\n" +
            "                            </div>");
        if("1"==payment_logo){
            $(".yingshou").html("应付");
        }
    }
    //押金
    if ("1" == type) {
        $(".yajin").html("<div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">房屋应收押金</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" name=\"TOTAL_PRICE\" id=\"BOND\" lay-verify=\"title\" placeholder=\"\"\n" +
            "                                           class=\"layui-input\" readonly>\n" +
            "                                    <span class=\"label-money\">元</span>\n" +
            "                                </div>\n" +
            "                            </div>\n" +
            "                            <div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">收费类型</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" class=\"layui-input\" id=\"select1\" name=\"COLLECTION_TYPE\" placeholder=\"\" readonly>\n" +
            "                                    <span class=\"label-money\"></span>\n" +
            "                                </div>\n" +
            "                            </div>");
    }
    //水费
    if ("2" == type) {
        $(".shuifei").html("<div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">应收水费</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" name=\"TOTAL_PRICE\"  id=\"yssf\" lay-verify=\"title\" placeholder=\"\" class=\"layui-input\" readonly>\n" +
            "                                    <span class=\"label-money\">元</span>\n" +
            "                                </div>\n" +
            "                            </div>\n" +
            "                            <div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">收费类型</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" class=\"layui-input\" id=\"select1\" name=\"COLLECTION_TYPE\" placeholder=\"\" readonly>\n" +
            "                                    <span class=\"label-money\"></span>\n" +
            "                                </div>\n" +
            "                            </div>");
        $(".sfcbsj").html("<div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">抄表时间</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" name=\"\" lay-verify=\"title\" id=\"cbsj\" autocomplete=\"off\"\n" +
            "                                           placeholder=\"抄表时间\" class=\"layui-input\" readonly>\n" +
            "                                </div>\n" +
            "                            </div>");
    }
    //电费
    if ("3" == type) {
        $(".dianfei").html("<div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">应收电费</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" name=\"TOTAL_PRICE\" id=\"ysdf\" lay-verify=\"title\" placeholder=\"\" class=\"layui-input\" readonly>\n" +
            "                                    <span class=\"label-money\">元</span>\n" +
            "                                </div>\n" +
            "                            </div>\n" +
            "                            <div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">收费类型</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                   <input type=\"text\" class=\"layui-input\" id=\"select1\" name=\"COLLECTION_TYPE\" placeholder=\"\" readonly>\n" +
            "                                    <span class=\"label-money\"></span>\n" +
            "                                </div>\n" +
            "                            </div>");
        $(".dbcbsj").html("<div class=\"layui-col-xs6 \">\n" +
            "                                <label class=\"layui-form-label1\">抄表时间</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" name=\"\" lay-verify=\"title\" id=\"cbsj\" autocomplete=\"off\"\n" +
            "                                           placeholder=\"抄表时间\" class=\"layui-input\" readonly>\n" +
            "                                </div>\n" +
            "                            </div>");
    }
    if ("4" == type) {
        $(".rqf").html("<div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">应收费</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                    <input type=\"text\" name=\"TOTAL_PRICE\" id=\"rqf\" lay-verify=\"title\" placeholder=\"\" class=\"layui-input\" readonly>\n" +
            "                                    <span class=\"label-money\"></span>\n" +
            "                                </div>\n" +
            "                            </div>\n" +
            "                            <div class=\"layui-col-xs6\">\n" +
            "                                <label class=\"layui-form-label1\">收费类型</label>\n" +
            "                                <div class=\"layui-inline\">\n" +
            "                                   <input type=\"text\" class=\"layui-input\" id=\"select1\" name=\"COLLECTION_TYPE\" placeholder=\"\" readonly>\n" +
            "                                    <span class=\"label-money\"></span>\n" +
            "                                </div>\n" +
            "                            </div>");
    }
    /* if("0"!=isLATE&""!=isLATE&null!=isLATE){
         $(".zhinajin").html("<div class=\"layui-col-xs6\">\n" +
             "                                <label class=\"layui-form-label1\">滞纳金</label>\n" +
             "                                <div class=\"layui-inline\">\n" +
             "                                    <input type=\"text\" name=\"LATE_PRICE\" id=\"RENT\" lay-verify=\"title\" placeholder=\"\"\n" +
             "                                           class=\"layui-input\" value=\'"+isLATE+"\'>\n" +
             "                                    <span class=\"label-money\">元</span>\n" +
             "                                </div>\n" +
             "                            </div>\n" +
             "                            <div class=\"layui-col-xs6\">\n" +
             "                                <label class=\"layui-form-label1\">收费类型</label>\n" +
             "                                <div class=\"layui-inline\">\n" +
             "                                    <select name=\"ALLOT_BILL\" lay-verify=\"required\" lay-search=\"\">\n" +
             "                                    <option id=\"select1\" value=\"0\">公户</option>\n" +
             "                                    <option id=\"select2\" value=\"1\">私户</option>\n" +
             "                                </select>\n" +
             "                                    <span class=\"label-money\"></span>\n" +
             "                                </div>\n" +
             "                            </div>");
     }*/
    //渲染下拉框
    form.render("select");
    $(".cancel").click(function () {
        var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
        parent.layer.close(index); // 再执行关闭
    });
    //获取 账单基本信息
    $.ajax({
        url: "/billController/listBill/",
        data: {ID: billID},
        dataType: "json",
        type: "post",
        async: false,
        success: function (res) {
            if (res.errcode === "0") {
                //加载 账单基本信息
                $("#ID").val(res.data.ID);
                $("#ROOM_NUM").val(res.data.ROOM_NUM);
                $("#COMPANY_NAME").val(res.data.COMPANY_NAME);
                $("#START_DATE").val(res.data.START_DATE);
                $("#END_DATE").val(res.data.END_DATE);
                $("#PAY_DATE").val(res.data.PAY_DATE);
                $("#INFO").val(res.data.INFO);
                $("#ACT_PAY_DATE").val(res.data.ACT_PAY_DATE);//实际收租时间
                if (undefined == res.data.TOTAL_PRICE_TEMP) {
                    $("#RENT").val(res.data.TOTAL_PRICE);//将要修改的 金额
                } else {
                    $("#RENT").val(res.data.TOTAL_PRICE_TEMP);//将要修改的 金额
                }
                //判断是否有滞纳金
                if (undefined != res.data.LATE_PRICE) {
                    var ALLOT_BILL_LATE = "";
                    if ("0" == res.data.ALLOT_BILL_LATE) {
                        ALLOT_BILL_LATE = "公户";
                    } else {
                        ALLOT_BILL_LATE = "私户";
                    }
                    $(".zhinajin").html("<div class=\"layui-col-xs6\">\n" +
                        "                                <label class=\"layui-form-label1\">滞纳金&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>\n" +
                        "                                <div class=\"layui-inline\">\n" +
                        "                                    <input type=\"text\" name=\"TOTAL_PRICE\" id=\"RENT\" lay-verify=\"title\" placeholder=\"\"\n" +
                        "                                           class=\"layui-input\" value=" + res.data.LATE_PRICE + " readonly>\n" +
                        "                                    <span class=\"label-money\">元</span>\n" +
                        "                                </div>\n" +
                        "                            </div>\n" +
                        "                            <div class=\"layui-col-xs6\">\n" +
                        "                                <label class=\"layui-form-label1\">收费类型</label>\n" +
                        "                                <div class=\"layui-inline\">" +
                        "                                <input type=\"text\" class=\"layui-input\" id=\"select1\" name=\"ALLOT_BILL_LATE\" value=" + ALLOT_BILL_LATE + " placeholder=\"\" readonly>\n" +
                        "                                    <span class=\"label-money\"></span>\n" +
                        "                                </div>\n" +
                        "                            </div>");

                }
                $("#BOND").val(res.data.TOTAL_PRICE);
                $("#rqf").val(res.data.TOTAL_PRICE);
                $("#yssf").val(res.data.TOTAL_PRICE);
                $("#SIGNER").val(res.data.SIGNER);//签订人
                $("#ACT_PAY_DATE").val(res.data.ACT_PAY_DATE);//实际付款时间
                //绘制下拉框
                if ("公户" === res.data.ALLOT_BILL) {
                    $("#select1").val(res.data.ALLOT_BILL);
                    $("#IS_RECEIPT").attr("checked", "checked");
                }
                if ("私户" === res.data.ALLOT_BILL) {
                    $("#select1").val(res.data.ALLOT_BILL);
                    $("#select2").attr("selected", "selected");
                }
                form.render("select");
                //收费信息
                var zujin = res.data.TOTAL_PRICE;
                var yajin = res.data.BOND;
                var zafei = res.data.TOTAL_PRICE;
                $("#zujin").html(zujin);
                $("#yajin").html(yajin);
                if (typeof(zujin) == "undefined" || yajin == "undefined") {
                    zujin = 0;
                    yajin = 0;
                }
                $("#zafei").html(res.data.TOTAL_PRICE);
                $("#xiaoji").html((parseFloat(zujin) + parseFloat(yajin)));
                //加载附件
                var attachment = res.attachmentData;
                //$(".attachments").empty();
                if (!!attachment) {
                    for (var i = 0; i < attachment.length; i++) {
                        var str = '<div class="layui-input-block">\n' +
                            '          <span>' + (i + 1) + '</span>\n' +
                            '          <span>' + attachment[i].NAME + '</span>\n' +
                            '          <span><a href="http://' + window.location.host + '/' + attachment[i].PATH + '" target="_blank" class=\"layui-btn layui-btn-mini layui-btn-normal\">下载</a></span>\n' +
                            '      </div>';
                        $(".attachments").append(str);
                    }
                }

            } else if (res.errcode === "1") {
                layer.msg(res.errmsg, function () {
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.error(XMLHttpRequest.status);
            console.error(XMLHttpRequest.readyState);
            console.error(textStatus);
        }
    });
    //加载交易明细
    var data = {};
    data.ID = billID;
    //alert(billID);
    table.render({
        elem: '#jymx'
        , url: '/billController/listjymx'
        , where: data
        , cols: [[ //标题栏

            {field: 'CREATE_DATE', title: '交易时间', width: 180}
            , {field: 'NAME', title: '操作人', width: 120}
            , {field: 'PAY_METHOD', title: '交易方式', width: 100}
            , {field: 'PAY_PRICE', title: '交易金额', width: 120}
            , {field: 'PAY_TRANSACTION', title: '交易流水号', width: 120}
            , {field: 'COLLECTION_TYPE', title: '收费类型', width: 120}
            , {field: 'INFO', title: '备注', width: 180}
        ]]
        , id: ''
        , page: true
        , height: 'auto'
        , limit: '10'
    });
    //监听审核 通过
    form.on('submit(billpass)', function (data) {
        data = data.field;
        data.ID = billID;
        data.CHECK_STATE = '1';
        layer.msg('确认通过吗', {
            time: 0
            , btn: ['确定', '取消']
            , shade: 0.3
            , yes: function (index) {
                $.ajax({
                    url: "/billController/shenhebill",
                    data: data,
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            var i = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(i);  // 关闭修改房间页面
                            parent.layer.msg("审核成功", {offset: '150px'});   // 父页面弹出修改成功
                            //刷新 待审核账单
                            window.parent.frames[0].reloadBillTable();
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });


            }
        });
        return false;
    });
    //监听审核 驳回
    form.on('submit(billend)', function (data) {
        data = data.field;
        data.ID = billID;
        data.CHECK_STATE = '2';
        /*parent.layer.prompt({title: '请输入驳回原因', value: ' ', formType: 2}, function (text, index) {
            layer.msg('得到了'+text);
            layer.close(index);
        });*/
        //layer.msg('得到了'+val);

        //data.DETAILS=val;
        /*layer.msg('确认驳回吗', {
            time: 0
            , btn: ['确定', '取消']
            , shade: 0.3
            , yes: function (index) {
                /!*$.ajax({
                    url: "/billController/shenhebill",
                    data: data,
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            var i = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(i);  // 关闭修改房间页面
                            parent.layer.msg("驳回成功", {offset: '150px'});   // 父页面弹出修改成功
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });*!/
                //刷新 待审核账单
                window.parent.frames[0].reloadBillTable();
            }
        });
        return false;*/
    });

    //操作详情
    var data = {};
    data.BILL_ID = billID;
    //alert(billID);
    //alert(data.CONTRACT_ID);
    table.render({
        elem: '#historySH'
        , url: '/recordController/list'
        , where: data
        , cols: [[
            {field: 'DETAILS', event: 'setSign', title: '操作详情', width: 500}
            , {field: 'ALLOT_BILL', event: 'setSign', title: '收款账户', width: 180}
            , {field: 'OPERATOR', event: 'setSign', title: '操作人', width: 150}
            , {field: 'OPERA_TIME', event: 'setSign', title: '操作时间', width: 150}

        ]]
        , id: ''
        , page: true
        , height: 'auto'
        , limit: '10'
        , size: 'sm' //小尺寸的表格
    });


});

/**
 * 操作方法集合
 * 使用： operate.function()
 * param: obj传this，调用该方法的对象
 *
 */
var operate = {
    // 审核通过
    pass: function (obj) {
        var index = parent.layer.prompt({title: '请输入备注', value: ' ', formType: 2}, function (text, index) {
            //审核通过
            $.ajax({
                url: "/billController/shenhebill",
                data: {ID: billID, CHECK_STATE: 1, DETAILS: text},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0") {
                        var i = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(i);  // 关闭修改房间页面
                        parent.layer.msg("审核成功", {offset: '150px'});   // 父页面弹出修改成功
                        //刷新 待审核账单
                        window.parent.frames[0].reloadBillTable();
                        parent.layer.close(index);
                    } else if (res.errcode === "1") {
                        layer.msg(res.errmsg, function () {
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });

        });

        return false;
    },
    // 审核不通过
    reject: function (obj) {
        var index = parent.layer.prompt({title: '请输入驳回原因', value: ' ', formType: 2}, function (text, index) {
            //审核不通过
            $.ajax({
                url: "/billController/shenhebill",
                data: {ID: billID, CHECK_STATE: 2, DETAILS: text},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0") {
                        var i = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(i);  // 关闭修改房间页面
                        parent.layer.msg("驳回成功", {offset: '150px'});   // 父页面弹出修改成功
                        //刷新 待审核账单
                        window.parent.frames[0].reloadBillTable();
                        parent.layer.close(index);
                    } else if (res.errcode === "1") {
                        layer.msg(res.errmsg, function () {
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });

        });
        return false;
    },
    // 关闭窗口
    cancel: function (obj) {
        var i = parent.layer.getFrameIndex(window.name);
        parent.layer.close(i); // 关闭合同详情提示框
    }
};